﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommonTools.Class;
using System.Threading;

namespace CommonTools.FreeProxy
{
    public class Kuaidaili : IProxy
    {
        private string _ROOTURL = "http://www.kuaidaili.com";
        private List<string> proxystr = new List<string>();
        public IList<Proxy> GetProxy()
        {
            var proxy = new List<Proxy>();
            var roothtml = "";
            if (GetHtml(_ROOTURL+"/free", out roothtml)) {
                var catalog = HttpHelper.GetNodeFormHtml(roothtml, "//div[@class='tag_area2']")[0].ChildNodes.Where(x=>x.Name=="a").Select(x => x.Attributes["href"].Value);
                Console.WriteLine("获取代理分类成功");
                var mainwaits = new List<EventWaitHandle>();
                foreach (var c in catalog)
                {
                    var ch = new ManualResetEvent(false);
                    mainwaits.Add(ch);
                    var waits = new List<EventWaitHandle>();
                    var catalogurl = _ROOTURL + c;
                    for (var i = 1; i <= 10; i++)
                    {
                        var handler = new ManualResetEvent(false);
                        waits.Add(handler);
                        Thread t = new Thread(new ParameterizedThreadStart(Load));
                        t.Start(new Tuple<string, EventWaitHandle>(catalogurl + i.ToString(), handler));
                        Thread.Sleep(1500);
                    }
                    WaitHandle.WaitAll(waits.ToArray());
                    ch.Set();
                }
                WaitHandle.WaitAll(mainwaits.ToArray());
                foreach (var p in proxystr)
                {
                    var q = p.Split(':');
                    proxy.Add(new Proxy
                    {
                        IP = q[0],
                        Port = int.Parse(q[1])
                    });
                }
            }
            return proxy;
        }
        private void Load(object o)
        {
            Console.WriteLine(string.Format("线程{0}已启动", Thread.CurrentThread.ManagedThreadId));
            var p = (Tuple<string, EventWaitHandle>)o;
            var url = p.Item1;
            var html = "";
            if(GetHtml(url,out html))
            {
                Console.WriteLine("抓取代理页面成功");
                try
                {
                    var row = HttpHelper.GetNodeFormHtml(html, "//tbody/tr");
                    foreach (var n in row)
                    {
                        var td = n.ChildNodes.Where(x => x.Name == "td").ToArray();
                        proxystr.Add(string.Format("{0}:{1}", td[0].InnerText, td[1].InnerText));
                    }
                }
                catch (Exception)
                {
                    
                }   
            }
            p.Item2.Set();           
        }
        private bool GetHtml(string url,out string html)
        {
            var r = false;
            html = "";
            for(var i = 0; i < 5; i++)
            {
                try
                {
                    Console.WriteLine(string.Format("正在尝试第{0}次抓取网页：{1}", i + 1, url));
                    html = HttpHelper.HttpGet(url, "", "text/html");
                    if (!string.IsNullOrEmpty(html))
                    {
                        r = true;
                        return r;
                    }
                }catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);                    
                }
                finally
                {
                    //Console.WriteLine("1秒后重试");
                    Thread.Sleep(1000);
                }
            }
            return r;
        }
    }
}
